package com.itheima.config;

import com.itheima.auth.filter.JwtAuthenticationTokenFilter;
import com.itheima.auth.handle.MyAccessDeniedHandler;
import com.itheima.auth.handle.MyAuthenticationFailureHandler;
import com.itheima.auth.handle.MyAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * company: www.abc.com
 * Author: 29746
 * Create Data: 2020/11/21
 */
//开启基于方法的安全认证机制，也就是说在web层的controller启用注解机制的安全确认，
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Resource
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;


    @Resource
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Resource
    private MyAccessDeniedHandler myAccessDeniedHandler;

    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    //全局的密码校验器
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new  BCryptPasswordEncoder();
    }

    @Override
    //一般配置静态资源，不走过滤器了，不需要判断了
    public void configure(WebSecurity web) throws Exception {
        //swagger-ui放行
        web.ignoring().antMatchers("/v2/api-docs",
                "/webjars/**",
                "/swagger-resources/configuration/ui",
                "/swagger-resources",
                "/swagger-resources/configuration/security",
                "/swagger-ui.html"
        );
    }

    @Override
    //一般配置url，需要判断是否放行，做很多判断
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                //登录认证的处理
                .successHandler(myAuthenticationSuccessHandler)//成功的处理
                .failureHandler(myAuthenticationFailureHandler)//失败的处理
                .and()
                .exceptionHandling().accessDeniedHandler(myAccessDeniedHandler)
                .and()
                .authorizeRequests()//请求
                //登录相关匹配，login放行
                .antMatchers("/*login*").permitAll()
                //剩余的url请求需要登陆
                .anyRequest().authenticated()
                .and()
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .addFilterBefore(jwtAuthenticationTokenFilter,UsernamePasswordAuthenticationFilter.class);

    }
}
